
package com.umfwechat.util.email;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

/**
 * 发送邮件
 *
 * @author lunaris 2010-5-25下午02:31:06
 *
 */
public abstract class AbstractMailSender {

	public static final String MIME_TYPE = "GB18030";

	protected String serverHost;
	protected int serverPort;
	protected boolean isAuthen;
	protected String userName;
	protected String userPwd;
	// ====================================
	protected String from;// 发送者邮箱
	protected String to;// 设置收件人
	protected String cc;// 抄送人
	// =======================================
	protected String subject;// 邮件主题
	protected String txtContent;// 邮件内容
	protected List<File> attachFile;// 附件
	protected String timeout = "0";//发送超时时间

	// ===============================================================
	/**
	 * 根据不同的协议生成不同的属性配置
	 *
	 * @return
	 */
	public abstract Properties getProperties();

	public void sendMail() throws MessagingException {
		Authenticator auth = new Authenticator(this.userName, this.userPwd);
		Properties props = getProperties();
		Session session = Session.getInstance(props, auth);
		Message msg = createMessage(session);
		Transport.send(msg);
	}

	protected Message createMessage(Session session) throws MessagingException {
		Message msg = new MimeMessage(session);
		// 设置发件人
		msg.setFrom(new InternetAddress(from));
		// 设置收件人
		msg.setRecipients(RecipientType.TO, InternetAddress.parse(to));
		if (cc != null) {// 设置抄送人
			msg.setRecipients(RecipientType.CC, InternetAddress.parse(cc));
		}
		//设置主题
		msg.setSubject(subject == null ? "No subject" : subject);

		//设置正文
		MimeMultipart part = new MimeMultipart();
		//文本正文
		MimeBodyPart mbp = new MimeBodyPart();
		mbp.setContent("" + txtContent, "text/html;charset=GB2312");
		part.addBodyPart(mbp);
		// --添加附件
		if (attachFile != null && attachFile.size() > 0) {
			for (File file : attachFile) {
				BodyPart attachBody = new MimeBodyPart();
				FileDataSource ds = new FileDataSource(file);
				attachBody.setDataHandler(new DataHandler(ds));
				try {
					attachBody.setFileName(MimeUtility.encodeText(file.getName()));
				} catch (UnsupportedEncodingException e) {
					attachBody.setFileName(file.getName());
				}
				part.addBodyPart(attachBody);
			}
		}
		msg.setContent(part);
		return msg;
	}

	public String getServerHost() {
		return serverHost;
	}

	public void setServerHost(String serverHost) {
		this.serverHost = serverHost;
	}

	public int getServerPort() {
		return serverPort;
	}

	public void setServerPort(int serverPort) {
		this.serverPort = serverPort;
	}

	public boolean isAuthen() {
		return isAuthen;
	}

	public void setAuthen(boolean isAuthen) {
		this.isAuthen = isAuthen;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserPwd() {
		return userPwd;
	}

	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public String getTo() {
		return to;
	}

	public void setTo(String to) {
		this.to = to;
	}

	public String getCc() {
		return cc;
	}

	public void setCc(String cc) {
		this.cc = cc;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getTxtContent() {
		return txtContent;
	}

	public void setTxtContent(String txtContent) {
		this.txtContent = txtContent;
	}

	public List<File> getAttachFile() {
		return attachFile;
	}

	public void setAttachFile(List<File> attachFile) {
		this.attachFile = attachFile;
	}

	/**
	 * @return timeout : return the property timeout.
	 */
	public String getTimeout() {
		return timeout;
	}

	/**
	 * @param timeout : set the property timeout.
	 */
	public void setTimeout(String timeout) {
		this.timeout = timeout;
	}

}
